from django.shortcuts import render
from django.views import View
from django import http


# Create your views here.

# 使用JsonResponse响应json数据
class JsonParamView(View):
    def get(self, request):
        data = {
            'city': 'beijing',
            'subject': 'python'
        }
        print(request.hehe)
        return http.JsonResponse(data)


class Response1View(View):
    def get(self, request):
        # return http.HttpResponse(content='响应体', content_type='数据类型,默认text/html',status='状态码,默认200')
        return http.HttpResponse(content='响应体', content_type='text/html', status=200)


# request.META属性获取请求头headers中的数据
class HeaderParamView(View):
    def get(self, request):
        ret = request.META.get('CONTENT_TYPE')
        print(ret)
        return http.HttpResponse('ok')


# 测试提取表单的请求体数据
class FormDataParamView(View):
    def post(self, request):
        name = request.POST.get('name')
        age = request.POST.get('age')
        return http.HttpResponse(f'name:{name},age:{age}')


# 测试提取查询字符串
class QSParamView(View):
    def get(self, request):
        name = request.GET.get('name')
        age = request.GET.get('age')
        return http.HttpResponse(f'name:{name},age:{age}')
